package com.cortex.papaya.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.cortex.papaya.search.Category;
import com.cortex.papaya.search.Link;
import com.cortex.papaya.view.LinkView.OnClickDocumentView;
import com.cortex.papayasearch.R;

public class WebSiteView extends LinearLayout {
	
	private LinearLayout scrollDocuments;
	private WebView webView;
	private boolean isWebViewStopped = false;
	private OnSelectedDocumetListener onSelectedDocument;
	private OnWebSiteStateChangedListener onWebSiteStateChanged;
	
	public WebSiteView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public WebSiteView(Context context) {
		super(context);
	}

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();
		initListener();
	}
	private void init(){
		scrollDocuments = (LinearLayout)findViewById(R.id.scrollDocuments);
		webView = (WebView)findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setLoadWithOverviewMode(true);
		webView.getSettings().setUseWideViewPort(true);
	}
	private void initListener(){
		webView.setWebViewClient(new WebViewClient() {

			public void onPageFinished(WebView view, String url) {
				if(onWebSiteStateChanged != null && !isWebViewStopped){
					onWebSiteStateChanged.webSiteStateChanged(true);
				}
			}
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				if(onWebSiteStateChanged != null){
					onWebSiteStateChanged.webSiteStateChanged(false);
				}
				isWebViewStopped = false;
			}
		});
	}
	public boolean isWebViewStopped() {
		return isWebViewStopped;
	}
	public void setOnSelectedDocumentListener(OnSelectedDocumetListener selectedDocument){
		this.onSelectedDocument = selectedDocument;
	}
	public void setOnWebSiteStateChangedListener(OnWebSiteStateChangedListener onWebSiteStateChanged){
		this.onWebSiteStateChanged = onWebSiteStateChanged;
	}
	public void show(Category category,Link documnet){
		stopLoading();
		webView.loadUrl(documnet.getUrl());
		buildScrollDocuments(category);
	}
	public void stopLoading(){
		webView.stopLoading();
		isWebViewStopped = true;
	}
	public void setFocusableWeb(boolean isFoucs){
		webView.setFocusable(isFoucs);
	}
	private void buildScrollDocuments(Category category){
		clear();
		// calculate width and height of each small category in documents view
		int width= (int) getResources().getDimension(R.dimen.documentTabWidth);
		int height = (int) getResources().getDimension(R.dimen.documentTabHeight);;
		int margin = 10; // in pixels 
		LayoutInflater inflter = LayoutInflater.from(getContext());
		// create small category view with width and height calculated
		for (final Link doc : category.getLinks()){
//			PapayaDocumentView documentView = PapayaDocumentView.createDocumentView(scrollDocuments, doc,width,height);
			
			LinkView documentView = (LinkView)inflter.inflate(R.layout.view_link, null);
			int iconWidth = (int) getResources().getDimension(R.dimen.min_icon_width);
			int iconHeight = (int) getResources().getDimension(R.dimen.min_icon_height);
			documentView.setIconSize(iconWidth, iconHeight);
			documentView.setDocument(doc);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,height);
			params.leftMargin = margin;
			params.rightMargin = margin;
			params.topMargin = margin;
			params.bottomMargin = margin;
			documentView.setLayoutParams(params);
			// when category clicked the application changed documents view
			documentView.setOnClickViewListener(new OnClickDocumentView() {
				
				@Override
				public void onClick(LinkView view) {
					stopLoading();
					webView.loadUrl(view.getDocument().getUrl());
					if(onSelectedDocument != null){
						onSelectedDocument.selectedDocument(view.getDocument());
					}
				}
			});
			scrollDocuments.addView(documentView);
		}
		// calculate position of selected document 
//		int documentPosition = document.getIndex();
//		int positionToJump = width * documentPosition;
//		// jump to position of selected document
//		((HorizontalScrollView)findViewById(R.id.containerHorizontalScrollDocuments)).scrollTo(positionToJump, 0);
	}
	public void clear(){
		scrollDocuments.removeAllViews();
	}
	public boolean isDocumentsCleared(){
		return false;
	}
	
	public interface OnSelectedDocumetListener{
		void selectedDocument(Link document);
	}
	public interface OnWebSiteStateChangedListener{
		void webSiteStateChanged(boolean isFinished);
	}

}
